package com.haofengsoft.scce.db.biz;

import android.content.Context;
import com.haofengsoft.scce.ManageApplication;
import com.haofengsoft.scce.db.DaoSession;
import com.haofengsoft.scce.db.dao.AdvertInfoDao;
import com.haofengsoft.scce.db.dao.CollectingOperLogDao;
import com.haofengsoft.scce.db.entity.CollectingOperLog;
import de.greenrobot.dao.query.QueryBuilder;

import java.util.List;

/**
 * Created by admin on 2015/7/27.
 */
public class CollectingOperLogBiz {
    private static CollectingOperLogBiz instance;
    private static Context appContext;
    private CollectingOperLogDao dao;

    private CollectingOperLogBiz() {
    }

    public static CollectingOperLogBiz getInstance(Context context) {
        if (instance == null) {
            instance = new CollectingOperLogBiz();
            if (appContext == null) {
                appContext = context.getApplicationContext();
            }
            DaoSession daoSession = ManageApplication.getDaoSession(context);
            instance.dao = daoSession.getCollectingOperLogDao();
        }
        return instance;
    }

    //通过类型，以及类型id
    public CollectingOperLog gainCollectingOperLogByTypeWithId(int modelType, long modelId) {
        QueryBuilder qb = dao.queryBuilder();
        qb.where(CollectingOperLogDao.Properties.ModelType.eq(modelType));
        qb.where(CollectingOperLogDao.Properties.ModelId.eq(modelId));
        qb.limit(1);
        List<CollectingOperLog> logList = qb.list();
        if (null == logList || logList.size() < 1) {
            return null;
        } else {
            return logList.get(0);
        }
    }

    //操作收藏
    public void insertCollectingOperLog(int modelType, long modelId) {
        CollectingOperLog operLog = new CollectingOperLog();
        operLog.setModelId(modelId);
        operLog.setModelType(modelType);
        operLog.setCollectingState(1);
        dao.insertOrReplace(operLog);
    }

    //删除
    public void deleteById(long key) {
        dao.deleteByKey(key);
    }

}
